package com.treemon.meetingcore.session
{
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import com.treemon.meetingcore.event.AVEventBase;
	public class AudioEntity
	{
		public function AudioEntity(serverid:String, streamid:String)
		{
			_serverid = serverid;
			_streamid = streamid;
	    	try
	    	{
				_connection = new NetConnection();
				_connection.client =this;
				_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
				_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				_connection.connect(_serverid);
	    	}
	    	catch(e:Error)
	    	{
				trace("AudioEntity::AudioEntity error:"+e.message+"|name:" +e.name+"|id:"  +e.errorID);    		
	    	}
		}
		
		public function get serverid(): String
		{
			return _serverid;
		}
		
		public function get streamid(): String
		{
			return _streamid;
		}
		
		private function netStatusHandler(event:NetStatusEvent):void 
		{
            switch (event.info.code) 
            {
                case "NetConnection.Connect.Success":
                    audio_player();
                    break;
                case "NetStream.Play.StreamNotFound":
                	trace("Error: Audio Stream Not Found" + _serverid + _streamid);
                    break;
            }
        }

	
	    private function securityErrorHandler(event:SecurityErrorEvent):void 
	    { 
			trace("securityErrorHandler: " + event); 
	    } 
		
		private function asyncErrorHandler(event:AsyncErrorEvent):void 
		{ 
			trace("asyncErrorHandler: " + event); 
		} 
		
		private function audio_player():void
		{
			_video = new Video();	
			_video.width = 0;
			_video.height = 0;
			_stream = new NetStream(_connection);
			_stream.receiveVideo(false);
			_stream.client=this; 
		   	_video.attachNetStream(_stream);
		   	_stream.bufferTime = AVEventBase.DELAY_TIME;
	   		_stream.play(_streamid);
	    }
	    
	    public function onBWDone (): void
	    {
	    	trace("onBWDone:test");
	    }
    
	    private function onMetaData(info:Object): void 
	    {
			trace("width: " + info.width);
			trace("height: " + info.height);
		}
		
		private var _connection:NetConnection; 
		private var _stream:NetStream; 
		private var _video:Video;
		private var _serverid:String;
		private var _streamid:String;
		private const _width:int = 0;
		private const _height:int = 0;

	}
}